package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"sync"
	"time"
)

// fetchURL 函数用于访问指定的 URL 并打印状态码
func fetchURL(url string, wg *sync.WaitGroup, attempt int) {
	defer wg.Done()

	client := http.Client{
		Timeout: 5 * time.Second,
	}

	resp, err := client.Get(url)
	if err != nil {
		fmt.Printf("第 %d 次访问 %s 时出错: %v\n", attempt, url, err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("第 %d 次读取 %s 响应体时出错: %v\n", attempt, url, err)
		return
	}
	fmt.Printf("第 %d 次访问 URL: %s, 状态码: %d, 响应体长度: %d\n", attempt, url, resp.StatusCode, len(body))
}

func main() {
	urls := []string{
		"http://172.1.4.99/", // 请替换为合法可测试的 URL
	}
	numAttempts := 1000000

	// 增加请求次数以模拟高并发
	var wg sync.WaitGroup

	for _, url := range urls {
		for i := 1; i <= numAttempts; i++ {
			wg.Add(1)
			go fetchURL(url, &wg, i)
		}
	}
	wg.Wait()
	fmt.Println("所有请求已完成")
}
